package View;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;

import java.awt.Font;

import javax.swing.SwingConstants;

import Controller.DB;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;

public class RegisterPanel extends JPanel {
	private JTextField email;
	private JTextField email_confirm;
	private JTextField street;
	private JTextField city;
	private JTextField state;
	private JTextField zip;
	private JTextField phone;
	private JPasswordField password;
	private JPasswordField password_confirm;
	private JLabel emailWarningLabel;
	private JLabel passwordWarningLabel;
	private JTextField name;
	private JLabel lblNamefirstLast;
	private RestUWantGUI parent;

	/**
	 * Create the panel.
	 */
	public RegisterPanel(final RestUWantGUI parent) {
		setLayout(null);
		this.parent = parent;
		email = new JTextField();
		email.setBounds(142, 121, 134, 28);
		add(email);
		email.setColumns(10);
		
		email_confirm = new JTextField();
		email_confirm.setColumns(10);
		email_confirm.setBounds(291, 121, 134, 28);
		add(email_confirm);
		
		street = new JTextField();
		street.setBounds(142, 227, 134, 28);
		add(street);
		street.setColumns(10);
		
		city = new JTextField();
		city.setColumns(10);
		city.setBounds(142, 267, 134, 28);
		add(city);
		
		state = new JTextField();
		state.setColumns(10);
		state.setBounds(291, 267, 55, 28);
		add(state);
		
		zip = new JTextField();
		zip.setColumns(10);
		zip.setBounds(357, 267, 93, 28);
		add(zip);
		
		phone = new JTextField();
		phone.setColumns(10);
		phone.setBounds(142, 326, 134, 28);
		add(phone);
		
		JButton btnCreateAccount = new JButton("Create Account");
		btnCreateAccount.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (verifyForm() && createAccount()) {
					parent.toLogin();
					JOptionPane.showMessageDialog(null, "Account created. Please log in with your new account info",
													"Account Created", JOptionPane.INFORMATION_MESSAGE);		
				}
			}
		});
		btnCreateAccount.setBounds(291, 327, 153, 25);
		add(btnCreateAccount);
		
		password = new JPasswordField();
		password.setBounds(142, 187, 134, 28);
		add(password);
		
		password_confirm = new JPasswordField();
		password_confirm.setBounds(291, 187, 134, 28);
		add(password_confirm);
		
		JLabel lblEnterEmailAnd = new JLabel("Enter Email and Confirm");
		lblEnterEmailAnd.setFont(new Font("Dialog", Font.BOLD, 10));
		lblEnterEmailAnd.setBounds(6, 127, 134, 15);
		add(lblEnterEmailAnd);
		
		JLabel lblPasswordAndConfirm = new JLabel("Password and Confirm");
		lblPasswordAndConfirm.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPasswordAndConfirm.setFont(new Font("Dialog", Font.BOLD, 10));
		lblPasswordAndConfirm.setBounds(0, 194, 134, 15);
		add(lblPasswordAndConfirm);
		
		JLabel lblStreetAddress = new JLabel("Street Address");
		lblStreetAddress.setHorizontalAlignment(SwingConstants.RIGHT);
		lblStreetAddress.setFont(new Font("Dialog", Font.BOLD, 10));
		lblStreetAddress.setBounds(0, 234, 134, 15);
		add(lblStreetAddress);
		
		JLabel lblCityStateZip = new JLabel("City, State, ZIP");
		lblCityStateZip.setFont(new Font("Dialog", Font.BOLD, 10));
		lblCityStateZip.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCityStateZip.setBounds(6, 274, 124, 15);
		add(lblCityStateZip);
		
		JLabel lblPhone = new JLabel("Phone");
		lblPhone.setFont(new Font("Dialog", Font.BOLD, 10));
		lblPhone.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPhone.setBounds(45, 333, 79, 15);
		add(lblPhone);
		
		emailWarningLabel = new JLabel("");
		emailWarningLabel.setFont(new Font("Dialog", Font.BOLD, 10));
		emailWarningLabel.setForeground(Color.RED);
		emailWarningLabel.setBounds(197, 94, 165, 15);
		add(emailWarningLabel);
		
		passwordWarningLabel = new JLabel("");
		passwordWarningLabel.setForeground(Color.RED);
		passwordWarningLabel.setFont(new Font("Dialog", Font.BOLD, 10));
		passwordWarningLabel.setBounds(197, 160, 165, 15);
		add(passwordWarningLabel);
		
		name = new JTextField();
		name.setColumns(10);
		name.setBounds(142, 54, 283, 28);
		add(name);
		
		lblNamefirstLast = new JLabel("Name (First Last)");
		lblNamefirstLast.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNamefirstLast.setFont(new Font("Dialog", Font.BOLD, 10));
		lblNamefirstLast.setBounds(0, 61, 134, 15);
		add(lblNamefirstLast);

	}
	private boolean createAccount() {
		 boolean success = true;
		   String sql = "insert into _445team11.Customer values " + "(?, ?, ?, ?, ?, ?, ?,?);";
	        PreparedStatement prepState = null;
	        try {
	            prepState = DB.conn.prepareStatement(sql);
	            prepState.setString(1, email.getText());
	            prepState.setString(2, name.getText());
	            prepState.setString(3, street.getText());
	            prepState.setString(4, city.getText());
	            prepState.setString(5, state.getText());
	            prepState.setString(6, zip.getText());
	            prepState.setString(7, phone.getText());
	            prepState.setString(8, password.getText());
	            prepState.executeUpdate();
	        } catch (SQLException e) {
	            System.out.println(e);
	            e.printStackTrace();
	            success = false;
	        }
	        return success;
	}
	
	private boolean verifyForm() {
		boolean res = true;
		
		if (!email.getText().equals(email_confirm.getText())) {
			res = false;
			emailWarningLabel.setText("Emails did not match");
		}
		
		if (!Arrays.equals(password.getPassword(), password_confirm.getPassword())) {
			res = false;
			passwordWarningLabel.setText("Passwords did not match");
		}
		
		String empty = "";
		if (email.getText().equals(empty) ||
			password.getPassword().length == 0 ||
			city.getText().equals(empty) ||
			street.getText().equals(empty) ||
			state.getText().equals(empty) ||
			zip.getText().equals(empty) ||
			phone.getText().equals(empty) ) {
			res = false;
			JOptionPane.showMessageDialog(null, "All fields are required. Please try again",
										"Missing Entries", JOptionPane.ERROR_MESSAGE);
		}
		return res;
	}
}
